package com.pds.web.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.common.utils.SecurityUtils;
import com.pds.web.monitor.domain.PdsMonitorTask;
import com.pds.web.monitor.domain.PdsMonitorTaskReport;
import com.pds.web.monitor.domain.PdsMonitorTaskSampling;
import com.pds.web.monitor.mapper.PdsMonitorTaskMapper;
import com.pds.web.monitor.mapper.PdsMonitorTaskReportMapper;
import com.pds.web.monitor.service.IPdsMonitorTaskReportService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class PdsMonitorTaskReportServiceImpl extends ServiceImpl<PdsMonitorTaskReportMapper, PdsMonitorTaskReport> implements IPdsMonitorTaskReportService {

    @Resource
    private PdsMonitorTaskReportMapper pdsMonitorTaskReportMapper;

//    @Resource
//    private PdsMonitorTaskMapper pdsMonitorTaskMapper;
//
//    /**
//     * 新增
//     *
//     * @param pdsMonitorTaskReport
//     * @return
//     */
//    @Override
//    public PdsMonitorTaskReport addReport(PdsMonitorTaskReport pdsMonitorTaskReport) {
////        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
////        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
//        Long userId = 1L;
//        Long deptId = 2L;
//
//        pdsMonitorTaskReport.setUserId(userId);
//        pdsMonitorTaskReport.setDeptId(deptId);
//
//        QueryWrapper<PdsMonitorTask> qw = new QueryWrapper<PdsMonitorTask>();
//        qw.eq("id", pdsMonitorTaskReport.getTaskId());
//        PdsMonitorTask pdsMonitorTask = pdsMonitorTaskMapper.selectOne(qw);
//
//        pdsMonitorTaskReport.setContractId(pdsMonitorTask.getContractId());
//        pdsMonitorTaskReport.setCompanyId(pdsMonitorTask.getCompanyId());
//        pdsMonitorTaskReport.setOrganizationId(pdsMonitorTask.getOrganizationId());
//
//        Date date = DateUtils.parseDate(DateUtils.getTime());
//
//        if (pdsMonitorTaskReport.getReportDate() == null) {
//            pdsMonitorTaskReport.setReportDate(date);
//        }
//        pdsMonitorTaskReport.setCreateTime(date);
//
//        pdsMonitorTaskReport.setStatus("1");
//
//        pdsMonitorTaskReportMapper.addReport(pdsMonitorTaskReport);
//
//        return pdsMonitorTaskReport;
//    }
//
//    /**
//     * 删除 -- 根据id
//     *
//     * @param id
//     * @return
//     */
//    @Override
//    public Boolean deleteReport(Long id) {
//        return pdsMonitorTaskReportMapper.deleteReport(id) > 0;
//    }
//
//    /**
//     * 更新
//     *
//     * @param pdsMonitorTaskReport
//     * @return
//     */
//    @Override
//    public Boolean updateReport(PdsMonitorTaskReport pdsMonitorTaskReport) {
//        Date date = DateUtils.parseDate(DateUtils.getTime());
//        pdsMonitorTaskReport.setUpdateTime(date);
//
//        return pdsMonitorTaskReportMapper.updateReport(pdsMonitorTaskReport) > 0;
//    }
//
//    /**
//     * 查询 -- 分页/条件：companyId，organizationId，reportName，reportDate，reportResult
//     *
//     * @param pdsMonitorTaskReport
//     * @return
//     */
//    @Override
//    public List<PdsMonitorTaskReport> selectReportList(PdsMonitorTaskReport pdsMonitorTaskReport) {
//        QueryWrapper<PdsMonitorTaskReport> wrapper = new QueryWrapper<PdsMonitorTaskReport>();
//
//        Long companyId = pdsMonitorTaskReport.getCompanyId();
//        Long organizationId = pdsMonitorTaskReport.getOrganizationId();
//        String reportResult = pdsMonitorTaskReport.getReportResult();
//        String reportName = pdsMonitorTaskReport.getReportName();
//        Date reportDate = pdsMonitorTaskReport.getReportDate();
//
//        if (!Objects.isNull(companyId)) {
//            wrapper.eq("company_id", companyId);
//        }
//        if (!Objects.isNull(organizationId)) {
//            wrapper.eq("organization_id", organizationId);
//        }
//        if (!Objects.isNull(reportResult) && !reportResult.isEmpty()) {
//            wrapper.eq("report_result", reportResult);
//        }
//        if (!Objects.isNull(reportName) && !reportName.isEmpty()) {
//            wrapper.like("report_name", reportName);
//        }
//        if (!Objects.isNull(reportDate)) {
//            wrapper.eq("report_date", reportDate);
//        }
//
//        wrapper.orderByDesc("update_time");
//        wrapper.orderByDesc("create_time");
//
//        return pdsMonitorTaskReportMapper.selectList(wrapper);
//    }

    /**
     * 更新
     *
     * @param pdsMonitorTaskReport
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean update(PdsMonitorTaskReport pdsMonitorTaskReport) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        pdsMonitorTaskReport.setUpdateTime(date);
        return pdsMonitorTaskReportMapper.updateById(pdsMonitorTaskReport) > 0;
    }

    /**
     * 查询
     *
     * @param pdsMonitorTaskReport
     * @return
     */
    @Override
    public PdsMonitorTaskReport selectReport(PdsMonitorTaskReport pdsMonitorTaskReport) {
        QueryWrapper<PdsMonitorTaskReport> wrapper = new QueryWrapper<>();

        Long taskId = pdsMonitorTaskReport.getTaskId();
        Long organizationId = null;

        if (!Objects.isNull(taskId)) {
            wrapper.eq("task_id", taskId);
        }
        if (!Objects.isNull(organizationId)) {
            wrapper.eq("organization_id", organizationId);
        }

        return pdsMonitorTaskReportMapper.selectOne(wrapper);
    }
}
